package com.dhl.cloud.core.service;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhl.cloud.common.Constants;
import com.dhl.cloud.common.util.DateUtils;
import com.dhl.cloud.common.util.PasswordUtils;
import com.dhl.cloud.common.vdo.UserInfoBean;
import com.dhl.cloud.common.vo.LoginFrontBean;
import com.dhl.cloud.core.dao.UserAndRoleMapper;
import com.dhl.cloud.core.dao.UserMapper;
import com.dhl.cloud.core.entity.User;
import com.dhl.cloud.core.entity.UserAndRole;

@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserAndRoleMapper userAndRoleMapper;
    
	public User selectByUsername(String username){
		User u = userMapper.selectByUsername(username);
		return u;
	}

	public List<UserAndRole> selectByUserrole(String username){
		List<UserAndRole> u = userAndRoleMapper.selectByusername(username);
		return u;
	}

	public List<UserAndRole> selectByUsernameAndTenantid(String username,String tenantid){
		List<UserAndRole> u = userAndRoleMapper.selectByusernameAndTenant(username,tenantid);
		return u;
	}
	
	public User selectByPhone(String phone){
		User u = userMapper.selectByPhone(phone);
		return u;
	}

	public User selectByCredentials(String credentials){
		User u = userMapper.selectByCredentials(credentials);
		return u;
	}

	//注册用户
	public int regeister(LoginFrontBean bean)
	{
		User user = new User();
		user.setUsername(bean.getUsername());
		user.setPersonname(bean.getPersonname()==null?bean.getUsername():bean.getPersonname());
		user.setPassword(PasswordUtils.encode(bean.getPassword(), Constants.SALT));
		user.setPhone(bean.getPhone());
		user.setCredentials(bean.getCredentials());
		user.setCreatetime(DateUtils.getDateTime());
		user.setUserimgurl("/Path/user.jpg");
		userMapper.insert(user);
		for (Integer id:bean.getRoleList())
		{
			UserAndRole userRole = new UserAndRole();
			userRole.setUsername(bean.getUsername());
			userRole.setRoleid(id);
			userAndRoleMapper.insert(userRole);
		}
		return 1;
		
	}

	public UserInfoBean getMyInfo(String username,String tenantid)
	{
		User u = userMapper.selectByUsername(username);

		UserInfoBean ub = new UserInfoBean();
		ub.setCredentials(u.getCredentials());
		ub.setId(u.getId());
		ub.setEmail(u.getEmail());
		ub.setPhone(u.getPhone());
		ub.setNickname(u.getNickname());
		ub.setUserimgurl(u.getUserimgurl());
		ub.setUsername(u.getUsername());
		ub.setUsersign(u.getUsersign());
		ub.setPersonname(u.getPersonname());
		ub.setSuperorgname(u.getSuperorgname());

//		//更加租户tenantid获取对应的机构name和专业name
//		//新增访问个人信心页面行为
//		userActionService.insert(tenantid,username,null,null,"进入个人信息");

		return ub;
	}

	public int saveMyInfo(String username,String nickname,String email,String usersign,String userimgurl,String personname)
	{
		User u = userMapper.selectByUsername(username);
		if (u != null)
		{
			u.setEmail(email);
			u.setNickname(nickname);
			u.setUsersign(usersign);
			u.setUserimgurl(userimgurl);
			u.setPersonname(personname);
			return userMapper.updateByPrimaryKeySelective(u);
		}
		return -1;
	}

//	@Transactional
//	public int saveMyPassword(String username,String password,String token)
//	{
//		User u = userMapper.selectByUsername(username);
//		if (u != null)
//		{
//			u.setPassword(PasswordUtils.encode(password, Constants.SALT));
//            int flag = userMapper.updateByPrimaryKeySelective(u);
////            if (iscreatepiuser == 1) {
////	            if (1==flag){
////	                //同步用户到deepinsight
////	                //http://192.168.1.164:6080/tyqxgl/rest/user2/add
////	                String url =deepUri+"/deep/updateUserPasswd";
////	                Map map = new HashMap();
////	                JSONObject obj = new JSONObject();
////	                //obj.put("username",username);
////	                obj.put("passwd", password);
////	                String flag1 = DeepInsightUtil.updateUserPasswd(url, obj,token);
////	
////	            }
////            }
//            return flag;
//
//		}
//		return -1;
//	}

	public int updateUserOnlineFlag(String username,Integer isonline){
		return userMapper.updateUserOnlineFlagByUsername(username,isonline);
	}

//	public PageInfo<OrgPersonStudyBean> getUsernamesByOrgIdWithPage(Integer orgid, String starttime, String endtime,Integer pageNum,Integer pageSize){
//		List<Org> orgs = orgMapper.getChildrenOrg(orgid);
//		List<Integer> orgids = ListUtils.extractFieldValue(orgs,"id");
//		PageHelper.startPage(pageNum,pageSize);
//		List<OrgPersonStudyBean> list =  userAndOrgMapper.getOrgPersonStudyList(orgids,starttime,endtime);
//		PageInfo<OrgPersonStudyBean> pageInfo = new PageInfo<>(list);
//		return pageInfo;
//	}
//
//	public List<OrgPersonStudyBean> getUsernamesByOrgId(Integer orgid, String starttime, String endtime){
//		List<Org> orgs = orgMapper.getChildrenOrg(orgid);
//		List<Integer> orgids = ListUtils.extractFieldValue(orgs,"id");
//		return userAndOrgMapper.getOrgPersonStudyList(orgids,starttime,endtime);
//	}
//
//	public List<SysUserStudyBean> getSysUserList(HttpUser user, SysUserStudyHourFrontBean studyHourFrontBean) {
//		List<SysUserStudyBean> sysUserStudyBeans = userMapper.getSysUserStudyHours(studyHourFrontBean.getUsernames(),user.getTenantid(),studyHourFrontBean.getStarttime(),studyHourFrontBean.getEndtime());
//		return sysUserStudyBeans;
//	}
}
